home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / src / timeconvert.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  2KB  |  96 lines

  1. #include "amiga.h"
  2. #include "timeconvert.h"
  3. #include <time.h>
  4. #include <proto/timer.h>
  5.  
  6. int _gettime(struct timeval *tp)
  7. {
  8. #ifdef USE_LOCAL
  9.     struct tm *local;
  10.     time_t gmt;
  11. #endif
  12.  
  13.     GetSysTime(tp);
  14.     /* Correct for different epoch (78 vs 70) */
  15.     tp->tv_secs += 252460800;
  16.  
  17. #ifdef USE_LOCAL
  18.     /* correct for timezone */
  19.     local = gmtime(&now.tv_secs);
  20.     local->tm_isdst = -1;    /* We don't know about dst */
  21.     gmt = mktime(local);
  22.  
  23.     if (gmt == -1) {
  24.     errno = EINVAL;
  25.     return -1;
  26.     }
  27.     tp->tv_secs = gmt;
  28. #endif
  29.     return 0;
  30. }
  31.  
  32. #ifdef USE_LOCAL
  33.  
  34. /* System is storing local time */
  35.  
  36. void _gmt2amiga(time_t time, struct DateStamp *date)
  37. {
  38.     struct tm *local, *gmt;
  39.  
  40.     local = localtime(&time);
  41.     gmt = gmtime(&time);
  42.     date->ds_Tick = 50 * local->tm_sec;
  43.     date->ds_Minute = local->tm_min + local->tm_hour * 60;
  44.  
  45.     /* Now calculate the day assuming we are in GMT */
  46.     date->ds_Days = (time - 252460800) / 86400;
  47.     /* And correct by comparing local with GMT */
  48.     if (local->tm_year < gmt->tm_year ||
  49.     local->tm_year == gmt->tm_year && local->tm_yday < gmt->tm_yday)
  50.     date->ds_Days--;
  51.     if (local->tm_year > gmt->tm_year ||
  52.     local->tm_year == gmt->tm_year && local->tm_yday > gmt->tm_yday)
  53.     date->ds_Days++;
  54. }
  55.  
  56. time_t _amiga2gmt(struct DateStamp *date)
  57. {
  58.     struct tm *local;
  59.     time_t secs;
  60.  
  61.     secs = date->ds_Tick / 50 + date->ds_Minute * 60 + date->ds_Days * 86400 +
  62.     252460800;
  63.     local = gmtime(&secs);
  64.     local->tm_isdst = -1;    /* We don't know about dst */
  65.  
  66.     return mktime(local);
  67. }
  68.  
  69. #else
  70.  
  71. /* System is storing GMT */
  72. /* Leap seconds are not handled !! */
  73.  
  74. void _gmt2amiga(time_t time, struct DateStamp *date)
  75. {
  76.     time_t time78, day_secs;
  77.  
  78.     time78 = time - 252460800;    /* Change Epoch */
  79.     date->ds_Days = time78 / 86400;
  80.     day_secs = time78 % 86400;
  81.     date->ds_Minute = day_secs / 60;
  82.     date->ds_Tick = 50 * (day_secs % 60);
  83. }
  84.  
  85. time_t _amiga2gmt(struct DateStamp *date)
  86. {
  87.     time_t secs;
  88.  
  89.     secs = date->ds_Tick / 50 + date->ds_Minute * 60 + date->ds_Days * 86400 +
  90.     252460800;
  91.  
  92.     return secs;
  93. }
  94.  
  95. #endif
  96.